*ALL PAIRS, full sample


local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26								//number of times windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort window size						
local bc_threshold=100							//set backward citation threshold on citing patents

global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

est clear 

*===================================================
* TABLE 2
*===================================================

local controls_fes="i.time originality science_cites inventors_citing i.wipo_citing_broad i.wipo_cited_broad"

	
// JPO cites

local m="cited_jp_it_fc"

*take base set
use "$DATA\twincites_applicant_did_prep.dta", clear
label variable treated "Ave. Eff. Treated"

drop if backcite_count_citing>`bc_threshold'
drop if `m'==.

* drop all but first citation from each geographical source for each family
sort family_cited priority_citing_d
duplicates drop family_cited nonJP, force

*remove families that only had citations from one source
duplicates tag family_cited, gen(tag)
drop if tag==0
drop tag

*set up time parameter
gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
quietly: sum temp
gen time=temp-r(min)

local dep="lag"
gen mXtreated=treated*`m'
gen mXpost=`m'*after_change_cited
gen mXus=`m'*nonJP
local int="mXtreated treated mXpost mXus"

display "`m': triple diff"
xi: reg `dep' `int' `m' nonJP `controls_fes', vce(cluster inventor_country_citing_e)
estimates store trip_`m'

*Split sample

foreach h in 0 1 {
	use "$DATA\twincites_applicant_did_prep.dta", clear
	label variable treated "Ave. Eff. Treated"

	drop if backcite_count_citing>`bc_threshold'
	drop if `m'==.
	
	replace backcite_count_citing=ln(backcite_count_citing+1)
	quietly: sum backcite_count_citing
	replace backcite_count_citing=(backcite_count_citing-r(mean))/r(sd)

	sort `m'
	egen high=cut(`m'), group(2)
	drop if high!=`h'			

	*remove families that only had citations from one source
	sort family_cited priority_citing_d
	duplicates drop family_cited nonJP, force
	duplicates tag family_cited, gen(tag)
	drop if tag==0
	drop tag

	*set up time parameter
	gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
	quietly: sum temp
	gen time=temp-r(min)
	
	display "`m': `h'"
	
	xi: reg lag treated nonJP `m' `controls_fes', vce(cluster inventor_country_citing_e)
	estimates store split_`m'_`h'

	}
	
	
//output tables
esttab  trip_cited_jp_it_fc using "$OUT\triple_did_invspec.tex", replace ///
	b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
	keep(`int' nonJP cited_jp_it_fc ) ///
	mlabel( "JPO cites") alignment(D{.}{.}{-1})
	
foreach h in 0 1 {
	esttab split_cited_jp_it_fc_`h' using "$OUT\split_`h'_invspec.tex", replace ///
		b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
		keep(treated nonJP) ///
		mlabel( "JPO cites") alignment(D{.}{.}{-1})
}

*===================================================
* TABLE 3
*===================================================

local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26								//number of times windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort window size						
local bc_threshold=100							//set backward citation threshold on citing patents

est clear 

local controls_fes="i.time originality science_cites inventors_citing i.wipo_citing_broad i.wipo_cited_broad"


// Firm R&D scale (unique inventors in year of policy change)

local m="firm_size"

*Split sample

foreach h in 0 1 {
	use "$DATA\twincites_applicant_did_prep.dta", clear
	label variable treated "Ave. Eff. Treated"

	drop if backcite_count_citing>`bc_threshold'
	drop if `m'==.
	
	sort `m'
	egen high=cut(`m'), group(2)
	drop if high!=`h'		

	*remove families that only had citations from one source
	sort family_cited priority_citing_d
	duplicates drop family_cited nonJP, force
	duplicates tag family_cited, gen(tag)
	drop if tag==0
	drop tag

	*set up time parameter
	gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
	quietly: sum temp
	gen time=temp-r(min)
	
	display "`m': `h'"
	
	xi: reg lag treated nonJP `m' `controls_fes', vce(cluster inventor_country_citing_e)
	estimates store split_`m'_`h'
	}		


// Firm JP patenting, pre-AIPA

local m="JP_app_preAIPA"


foreach h in 0 1 {
	use "$DATA\twincites_applicant_did_prep.dta", clear
	label variable treated "Ave. Eff. Treated"

	drop if backcite_count_citing>`bc_threshold'
	drop if `m'==.
	replace JP_app_preAIPA=1 if nonJP==0
	
	drop if `m'!=`h' & nonJP==1
		
	*remove families that only had citations from one source
	sort family_cited priority_citing_d
	duplicates drop family_cited nonJP, force
	duplicates tag family_cited, gen(tag)
	drop if tag==0
	drop tag

	*set up time parameter
	gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
	quietly: sum temp
	gen time=temp-r(min)
	
	display "`m': `h'"
	
	xi: reg lag treated nonJP `controls_fes', vce(cluster inventor_country_citing_e)
	estimates store split_`m'_`h'
	}	

//output tables
	
foreach h in 0 1 {
	esttab split_firm_size_`h' split_JP_app_preAIPA_`h' using "$OUT\split_`h'_approp.tex", replace ///
		b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
		keep(treated nonJP) ///
		mlabel("R\&D scale" "Prior JPO app.") alignment(D{.}{.}{-1})
}












